package tool.generatesql;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 处理成sql语句用于刷新数据
 * @Author: yjj
 * @Date: 2025/4/29 16:12
 */
public class Main {
    public static void main(String[] args) throws UnsupportedEncodingException {
        List<String> list = getTxt("src/tool/generatesql/demo.txt");
        for (String s : list) {
            String[] split = s.split("\t");
            String diskUsage = split[2];
            if ("NULL".equals(diskUsage)) {
                diskUsage = "0";
            }
            BigDecimal b1 = new BigDecimal(diskUsage);
            BigDecimal b2 = new BigDecimal("1024");
            String size = b1.divide(b2, 4, 4).toString();
            Integer status = 2;
            if (split[1].equals("available")) {
                status = 2;
            }
            if (split[1].equals("creating")) {
                status = 1;
            }
            if (split[1].equals("deleting")) {
                status = 6;
            }

            System.out.println("update backup_copy set status = " + status + ", backup_size = " + size +", uuid = '" + split[0] + "' where name = '" + split[3].substring(0, 17) + "';");
        }
    }

    public static List<String> getTxt(String filePath) {
        List<String> list = new LinkedList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            // 按行读取文件内容
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static boolean isValidUUID(String uuid) {
        try {
            if (uuid == null) {
                return false;
            }
            // 尝试解析为 UUID
            UUID.fromString(uuid);
            return true;
        } catch (IllegalArgumentException e) {
            // 如果解析失败，则不是合法的 UUID
            return false;
        }
    }
}
